﻿var list = new Array();
$(function () {
    //初始化表格
    var initializationDataTable = function (dataResult) {
        $('#datalist-table').dataTable({
            "autoWidth": true,
            "oLanguage": {
                "sProcessing": "正在加载数据...",
                "sZeroRecords": "没有您要搜索的内容",
                "sInfo": "从_START_ 到 _END_ /共 _TOTAL_ 条",
                "sInfoEmpty": "记录数为0",
                "sInfoFiltered": "(全部记录数 _MAX_  条)",
                "sInfoPostFix": "",
                "sUrl": "",
                "oPaginate": {
                    "sFirst": "第一页",
                    "sPrevious": " 上一页 ",
                    "sNext": " 下一页 ",
                    "sLast": " 最后一页 "
                }
            },
            paging: false,
            ordering:  false,
            bStateSave: true,
            //"sAjaxSource": apiUrl,
            //"fnServerData": function(sSource, aoData, fnCallback) {
            //    console.log(JSON.stringify(list));
            //    fnCallback(list);
            //},
            //"aoColumns": [
            //    { "sName": "No" },
            //    { "sName": "Id" },
            //    { "sName": "Code" },
            //    { "sName": "Caption" }
            //],
            data: dataResult,
            columns: [
                { data: 'Code' },
                { data: 'Caption' },
                { data: 'Id' }
            ],//定义自定义
            "columnDefs": [
                {
                    "targets": [2], // 目标列位置，下标从0开始
                    "data": "name", // 数据列名
                    "render": function (resultdata, type, full) { // 返回自定义内容
                        return "<a href='javascript:void(0)' onclick=\"AddData('" + resultdata + "')\"><i class='fa  fa-plus-circle   '></i>添加子集</a>&nbsp;<a href='javascript:void(0)' onclick=\"updateData('" + resultdata + "')\"><i class='fa  fa-edit'></i>修改</a>&nbsp;<a href='javascript:void(0)' onclick=\"DeleteData('" + resultdata + "')\"><i class='fa  fa-times-circle'></i>删除</a>";
                    }
                }
            ]//定义其他的格式
        });
    }
   //获取表格所需的数据并填充到list数组 最后初始化表格
    function getData() {
        $.ajax({
            url: apiUrl,
            type: "Get",
            dataType: "json",
            async: false,
            data: { "sys:sno": "ETL", "sys:act": "GetDataServiceFolderTree" },
            success: function (data) {
                if (data.IsSccuess) {
                    if (data.Data.Items != null && data.Data.Items.length != 0) {
                        list = new Array();
                        combinedData(data.Data.Items, 0, "", 1);
                        initializationDataTable(list);
                    }
                } else {
                    if (IsLogined(data.ErrorCode)) {
                        alert("错误代码：" + data.ErrorCode + "，错误信息：" + data.Message);
                    }
                }
            },
            error: function () {
                alert("系统异常，请稍后重试");
            }
        });
    }
    //递归查找数据并组合成列表
    var combinedData = function (source, i, partentId) {
        for (var j = 0; j < source.length; j++) {
            var str = "";
            for (var k = 0; k < i; k++) {
                str += "&nbsp;&nbsp;";
            }
            list.push({ "Caption": str + source[j].Caption, "Code": str + source[j].Code, "Id": source[j].Id, "parentId": partentId });
            if (source[j].Items != null && source[j].Items.length > 0) {
                combinedData(source[j].Items, i + 1, source[j].Id);
            }
        }
    }
    //更新服务目录select下拉框
    var updateDataServiceFolderTree = function () {
        $("#update-FolderId").html("  <option value='00000000-0000-0000-0000-000000000000'>根目录<\/option>");
        $("#add-FolderId").html("  <option value='00000000-0000-0000-0000-000000000000'>根目录<\/option>");
        var list = GetDataServiceFolderTree();
        $("#add-FolderId").append(list);
        $("#update-FolderId").append(list);
    }
    //新增数据服务目录表单
    $("#AddModelForm").submit(function () {
        $.ajax({
            url: apiUrl,
            data: $(this).serialize(),
            type: "Post",
            dataType: "json",
            success: function (data) {
                if (data.IsSccuess) {
                    alert("新增成功");
                    window.location.reload();
                } else {
                    if (IsLogined(data.ErrorCode)) {
                        alert(data.Message);
                    }
                }
            },
            beforeSend: function () {
                // $("btn_login").html("登录中...");
            }
        });
        return false;
    });
    //提交更新数据服务目录表单
    $("#UpdateModelForm").submit(function () {
        $.ajax({
            url: apiUrl,
            type: "Post",
            dataType: "json",
            async: false,
            data: $(this).serialize(),
            success: function (data) {
                if (data.IsSccuess) {
                    alert("编辑成功");
                    window.location.reload();
                } else {
                    if (IsLogined(data.ErrorCode)) {
                        alert("错误代码：" + data.ErrorCode + "，错误信息：" + data.Message);
                    }
                }
            },
            beforeSend: function () {

            },
            error: function () {
                alert("系统异常，请稍后重试");
            }
        });
    });

    updateDataServiceFolderTree();
    getData();

});

//删除单条数据服务目录
var DeleteData = function (id) {
    if (confirm("是否删除？")) {
        $.ajax({
            url: apiUrl,
            type: "Post",
            dataType: "json",
            async: false,
            data: { "sys:sno": "ETL", "sys:act": "DeleteDataServiceFolder", Id: id },
            success: function (data) {
                if (data.IsSccuess) {
                    alert("删除成功");
                    window.location.reload();

                } else {
                    if (IsLogined(data.ErrorCode)) {
                        alert("错误代码：" + data.ErrorCode + "，错误信息：" + data.Message);
                    }
                }
            },
            beforeSend: function () {

            },
            error: function () {
                alert("系统异常，请稍后重试");
            }
        });
    }

}
//新增数据服务目录表单操作
var AddData = function (id) {
    $("#add-FolderId").val(id);
    $('#AddModel').modal({
        show: true
    });
}
//更新数据服务目录表单操作
var updateData = function (id) {
    for (var i = 0; i < list.length; i++) {
        if (id == list[i].Id) {
            $("#update-FolderId").val(list[i].parentId);
            if (list[i].parentId=="") {
                $("#update-FolderId").val("00000000-0000-0000-0000-000000000000");
            }
            $("#update-Folder-Code").val(list[i].Code.replace("&nbsp;&nbsp;",""));
            $("#update-Folder-Caption").val(list[i].Caption.replace("&nbsp;&nbsp;", ""));
        }
    }
    $("#update-Folder-Id").val(id);
    $('#UpdateModel').modal({
        show: true
    });
}


